package com.practice.Serialize.Client;

import com.practice.Serialize.pojo.SubscribeReq;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

import java.util.Timer;
import java.util.TimerTask;

public class SubReqClientHandler extends ChannelHandlerAdapter {
    private int counter;

    @Override
    public void channelActive(final ChannelHandlerContext ctx) throws Exception {
        new Timer().schedule(
                new TimerTask() {
                    @Override
                    public void run() {
                        ctx.writeAndFlush(subReq(++counter));
                    }
                }, 0, 1000);


    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("The Response of server is MSG:[" + msg + "]");
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }


    private SubscribeReq subReq(int id) {
        SubscribeReq req = new SubscribeReq();
        req.setSubReqID(id);
        req.setAddress("四川XXXX");
        req.setPhoneNumber("13888888888");
        req.setProductName("RMB");
        req.setUserName("Crowhyc");
        req.setSendTime(System.currentTimeMillis());

        return req;
    }
}
